package com.dzu.easy;

import java.util.*;

/**
 * @author ZhaoDong
 * @date 2023/2/1 10:21
 * @description 349. 两个数组的交集
 */
public class Test349 {
    public static void main(String[] args) {
        int[] ar1 = new int[]{1, 2, 3};
        int[] ar2 = new int[]{3, 2, 3};
        System.out.println(Arrays.toString(intersection(ar1, ar2)));
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> st1 = new HashSet<>();
        for (int i : nums1) {
            st1.add(i);
        }

        Set<Integer> st2 = new HashSet<>();
        for (int i : nums2) {
            if (st1.contains(i)) {
                st2.add(i);
            }
        }

        int[] arr = new int[st2.size()];
        int j = 0;
        for (Integer i : st2) {
            arr[j++] = i;
        }
        return arr;
    }
}
